package com.cozy.android.sdk;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.cozy.android.sdk.ad.CozyMaxReward;
import com.cozy.android.sdk.bridge.CozyUnityBridge;
import com.cozy.android.sdk.utils.CozyAesUtils;

public class CozySDKManager {

    public static CozySDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final CozySDKManager GAME_APPLICATION = new CozySDKManager();
    }


    public void init(Activity activity) {
        Log.e("CoMM", "onCreateonCreate");
        Context mApplication = activity.getApplicationContext();
        CozyBusiness.init(mApplication);
        CozyUnityBridge.getInstance().setMainGameActivity(activity);
    }

    public void interInit() {
        setHasUserConsent();
        setDoNotSell();

        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(getKey())
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        AppLovinSdk.getInstance(CozyBusiness.context).initialize(initConfig, config -> {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
            Log.e("CoMM", "onSdkInitialized");
            CozyMaxReward.getInstance().loadRewardAd(CozyBusiness.context);
        });
    }

    private String getKey() {
        return CozyAesUtils.decrypt(CozyConstant.MAX_KEY, CozyConstant.KEY_FLAG);
    }

    private void setDoNotSell() {
        AppLovinPrivacySettings.setDoNotSell(false);
    }

    private void setHasUserConsent() {
        AppLovinPrivacySettings.setHasUserConsent(true);
    }
}
